home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / zcolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-21  |  6.2 KB  |  237 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994, 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zcolor.c */
  20. /* Color operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "estack.h"
  25. #include "ialloc.h"
  26. #include "igstate.h"
  27. #include "iutil.h"
  28. #include "store.h"
  29. #include "gxfixed.h"
  30. #include "gxmatrix.h"
  31. #include "gzstate.h"
  32. #include "gxdevice.h"
  33. #include "gxcmap.h"
  34. #include "icolor.h"
  35.  
  36. /* Import the 'for' operator */
  37. extern int
  38.   zfor_fraction(P1(os_ptr));
  39.  
  40. /* Imported from gsht.c */
  41. void gx_set_effective_transfer(P1(gs_state *));
  42.  
  43. /* Define the number of stack slots needed for zcolor_remap_one. */
  44. const int zcolor_remap_one_ostack = 4;
  45. const int zcolor_remap_one_estack = 3;
  46.  
  47. /* - currentalpha <alpha> */
  48. private int
  49. zcurrentalpha(register os_ptr op)
  50. {    push(1);
  51.     make_real(op, gs_currentalpha(igs));
  52.     return 0;
  53. }
  54.  
  55. /* - currentgray <gray> */
  56. private int
  57. zcurrentgray(register os_ptr op)
  58. {    push(1);
  59.     make_real(op, gs_currentgray(igs));
  60.     return 0;
  61. }
  62.  
  63. /* - currentrgbcolor <red> <green> <blue> */
  64. private int
  65. zcurrentrgbcolor(register os_ptr op)
  66. {    float par[3];
  67.     gs_currentrgbcolor(igs, par);
  68.     push(3);
  69.     make_reals(op - 2, par, 3);
  70.     return 0;
  71. }
  72.  
  73. /* - currenttransfer <proc> */
  74. private int
  75. zcurrenttransfer(register os_ptr op)
  76. {    push(1);
  77.     *op = istate->transfer_procs.colored.gray;
  78.     return 0;
  79. }
  80.  
  81. /* - processcolors <int> - */
  82. /* Note: this is an undocumented operator that is not supported */
  83. /* in Level 2. */
  84. private int
  85. zprocesscolors(register os_ptr op)
  86. {    push(1);
  87.     make_int(op, gs_currentdevice(igs)->color_info.num_components);
  88.     return 0;
  89. }
  90.  
  91. /* <alpha> setalpha - */
  92. private int
  93. zsetalpha(register os_ptr op)
  94. {    float alpha;
  95.     int code;
  96.     if ( real_param(op, &alpha) < 0 )
  97.       return_op_typecheck(op);
  98.     if ( (code = gs_setalpha(igs, alpha)) < 0 )
  99.       return code;
  100.     pop(1);
  101.     return 0;
  102. }
  103.  
  104. /* <gray> setgray - */
  105. private int
  106. zsetgray(register os_ptr op)
  107. {    float gray;
  108.     int code;
  109.     if ( real_param(op, &gray) < 0 )
  110.       return_op_typecheck(op);
  111.     if ( (code = gs_setgray(igs, gray)) < 0 )
  112.       return code;
  113.     make_null(&istate->colorspace.array);
  114.     pop(1);
  115.     return 0;
  116. }
  117.  
  118. /* <red> <green> <blue> setrgbcolor - */
  119. private int
  120. zsetrgbcolor(register os_ptr op)
  121. {    float par[3];
  122.     int code;
  123.     if (    (code = num_params(op, 3, par)) < 0 ||
  124.         (code = gs_setrgbcolor(igs, par[0], par[1], par[2])) < 0
  125.        )
  126.         return code;
  127.     make_null(&istate->colorspace.array);
  128.     pop(3);
  129.     return 0;
  130. }
  131.  
  132. /* <proc> settransfer - */
  133. private int
  134. zsettransfer(register os_ptr op)
  135. {    int code;
  136.     check_proc(*op);
  137.     check_ostack(zcolor_remap_one_ostack - 1);
  138.     check_estack(1 + zcolor_remap_one_estack);
  139.     istate->transfer_procs.colored.red =
  140.       istate->transfer_procs.colored.green =
  141.       istate->transfer_procs.colored.blue =
  142.       istate->transfer_procs.colored.gray = *op;
  143.     code = gs_settransfer_remap(igs, gs_mapped_transfer, false);
  144.     if ( code < 0 ) return code;
  145.     push_op_estack(zcolor_reset_transfer);
  146.     pop(1);  op--;
  147.     return zcolor_remap_one(&istate->transfer_procs.colored.gray, op,
  148.                 igs->set_transfer.colored.gray, igs,
  149.                 zcolor_remap_one_finish);
  150. }
  151.  
  152. /* ------ Internal routines ------ */
  153.  
  154. /* Prepare to remap one color component */
  155. /* (also used for black generation and undercolor removal). */
  156. /* Use the 'for' operator to gather the values. */
  157. /* The caller must have done the necessary check_ostack and check_estack. */
  158. int
  159. zcolor_remap_one(const ref *pproc, register os_ptr op, gx_transfer_map *pmap,
  160.   const gs_state *pgs, int (*finish_proc)(P1(os_ptr)))
  161. {    osp = op += 4;
  162.     make_int(op - 3, 0);
  163.     make_int(op - 2, 1);
  164.     make_int(op - 1, transfer_map_size - 1);
  165.     *op = *pproc;
  166.     ++esp;
  167.     make_struct(esp, imemory_space((gs_ref_memory_t *)pgs->memory),
  168.             pmap);
  169.     push_op_estack(finish_proc);
  170.     push_op_estack(zfor_fraction);
  171.     return o_push_estack;
  172. }
  173.  
  174. /* Store the result of remapping a component. */
  175. private int
  176. zcolor_remap_one_store(os_ptr op, floatp min_value)
  177. {    int i;
  178.     gx_transfer_map *pmap = r_ptr(esp, gx_transfer_map);
  179.     if ( ref_stack_count(&o_stack) < transfer_map_size )
  180.       return_error(e_stackunderflow);
  181.     for ( i = 0; i < transfer_map_size; i++ )
  182.     {    float v;
  183.         int code =
  184.           real_param(ref_stack_index(&o_stack,
  185.                          transfer_map_size - 1 - i),
  186.                  &v);
  187.         if ( code < 0 ) return code;
  188.         pmap->values[i] =
  189.             (v < min_value ? float2frac(min_value) :
  190.              v >= 1.0 ? frac_1 :
  191.              float2frac(v));
  192.     }
  193.     ref_stack_pop(&o_stack, transfer_map_size);
  194.     esp--;                /* pop pointer to transfer map */
  195.     return o_pop_estack;
  196. }
  197. int
  198. zcolor_remap_one_finish(os_ptr op)
  199. {    return zcolor_remap_one_store(op, 0.0);
  200. }
  201. int
  202. zcolor_remap_one_signed_finish(os_ptr op)
  203. {    return zcolor_remap_one_store(op, -1.0);
  204. }
  205.  
  206. /* Finally, reset the effective transfer functions and */
  207. /* invalidate the current color. */
  208. int
  209. zcolor_reset_transfer(os_ptr op)
  210. {    gx_set_effective_transfer(igs);
  211.     return zcolor_remap_color(op);
  212. }
  213. int
  214. zcolor_remap_color(os_ptr op)
  215. {    gx_unset_dev_color(igs);
  216.     return 0;
  217. }
  218.  
  219. /* ------ Initialization procedure ------ */
  220.  
  221. BEGIN_OP_DEFS(zcolor_op_defs) {
  222.     {"0currentalpha", zcurrentalpha},
  223.     {"0currentgray", zcurrentgray},
  224.     {"0currentrgbcolor", zcurrentrgbcolor},
  225.     {"0currenttransfer", zcurrenttransfer},
  226.     {"0processcolors", zprocesscolors},
  227.     {"1setalpha", zsetalpha},
  228.     {"1setgray", zsetgray},
  229.     {"3setrgbcolor", zsetrgbcolor},
  230.     {"1settransfer", zsettransfer},
  231.         /* Internal operators */
  232.     {"1%zcolor_remap_one_finish", zcolor_remap_one_finish},
  233.     {"1%zcolor_remap_one_signed_finish", zcolor_remap_one_signed_finish},
  234.     {"0%zcolor_reset_transfer", zcolor_reset_transfer},
  235.     {"0%zcolor_remap_color", zcolor_remap_color},
  236. END_OP_DEFS(0) }
  237.